package com.lzh.edu.controller;


import com.lzh.commonutils.CommonResult;
import com.lzh.edu.entity.Chapter;
import com.lzh.edu.service.ChapterService;
import com.lzh.edu.vo.course.ChapterVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author lizuhu
 * @since 2021-04-24
 */
@CrossOrigin
@RestController
@RequestMapping("/edu/chapter")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    //课程大纲列表
    @ApiOperation(value = "获取课程大纲列表")
    @GetMapping("/getChapter")
    public CommonResult getCourseId(@RequestParam String courseId) {
        List<ChapterVo> list = chapterService.getChapterByCourseId(courseId);
        return CommonResult.ok().data("items",list);
    }

    @ApiOperation(value = "添加章节接口")
    @PostMapping("/saveChapter")
    public CommonResult saveChapter(@RequestBody Chapter chapter) {
        chapterService.save(chapter);
        return CommonResult.ok();
    }

    @ApiOperation(value = "根据id查询章节")
    @GetMapping("/getChapterById")
    public CommonResult getChapterById(@RequestParam String chapterId) {
        Chapter chapter = chapterService.getById(chapterId);
        return CommonResult.ok().data("item",chapter);
    }

    @ApiOperation(value = "根据id修改章节")
    @PutMapping("/updateChapter")
    public CommonResult updateChapter(@RequestBody Chapter chapter) {
        chapterService.updateById(chapter);
        return CommonResult.ok();
    }

    @ApiOperation(value = "根据id删除章节")
    @DeleteMapping("/deleteChapter")
    public CommonResult deleteChapterById(@RequestParam String chapterId) {
        Boolean result = chapterService.deleteChapterById(chapterId);
        if (result) return CommonResult.ok();
        else return CommonResult.error().message("本章包含小节无法删除，请删除小节后重试！");
    }
}

